package com.example.novel.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.novel.domain.*;
import com.example.novel.mapper.EvaluateMapper;
import com.example.novel.service.EvaluateService;
import com.example.novel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【evaluate_yj(评价表)】的数据库操作Service实现
* @createDate 2024-11-28 18:36:17
*/
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate>
    implements EvaluateService {

    @Autowired
    private UserService userService; ;

    @Override
    public Result<Evaluate> getEvaluate(Integer propertyId) {
// 查询所有用户，并转换为 Map<userId, Users>
        List<User> usersList = userService.query().list();
        Map<Integer, User> userMap = usersList.stream()
                .collect(Collectors.toMap(User::getId, user -> user));

// 查询评价数据，并过滤出 propertyId 匹配的记录
        List<Evaluate> evaluateList = list().stream()
                .filter(evaluate -> evaluate.getBookId().equals(propertyId))
                .collect(Collectors.toList());

// 为评价数据设置 userName 和 userImage
        for (Evaluate evaluate : evaluateList) {
            User user = userMap.get(evaluate.getUserId());
            if (user != null) {
                evaluate.setUserName(user.getUsername());
            }
        }

// 返回结果
        Result<Evaluate> result = new Result<>();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setDatas(evaluateList);
        return result;
    }

    @Override
    public Result getEvaluateById(String userId) {
        User user = userService.getById(userId);
        List<Evaluate> evaluateList = null;
        System.out.println(user);
        Result result = new Result();
        evaluateList = query().eq("user_id", userId).list();
        if (evaluateList.size() > 0) {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setDatas(evaluateList);
            return result;
        }
        return null;
    }

}




